<?php
/**
 * This is the model class for table "{{review}}".
 *
 * The followings are the available columns in table '{{review}}':
 * @property integer $id
 * @property integer $user_id
 * @property integer $object_id
 * @property integer $text
 * @property string $crdate
 * @property integer $rating_accuracy
 * @property integer $rating_communication
 * @property integer $rating_cleanliness
 * @property integer $rating_location
 * @property integer $rating_check_in
 * @property integer $rating_review
 */
class Review extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Review the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{review}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, object_id, text, crdate, rating_accuracy, rating_communication, rating_cleanliness, rating_location, rating_check_in, rating_review', 'required'),
			array('user_id, object_id, rating_accuracy, rating_communication, rating_cleanliness, rating_location, rating_check_in, rating_review', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, object_id, text, crdate, rating_accuracy, rating_communication, rating_cleanliness, rating_location, rating_check_in, rating_review', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'object' => array(self::BELONGS_TO, 'Object', 'object_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 			=>	Yii::t("main", "Id"),
			'user_id' 		=>	Yii::t("main", "User"),
			'object_id' 	=>	Yii::t("main", "Object"),
			'text' 			=>	Yii::t("main", "Text"),
			'crdate' 		=>	Yii::t("main", "Crdate"),
			'rating_accuracy' 		=>	Yii::t("main", "Accuracy"),
			'rating_communication' 	=>	Yii::t("main", "Communication"),
			'rating_cleanliness'	=>	Yii::t("main", "Cleanliness"),
			'rating_location' 		=>	Yii::t("main", "Location"),
			'rating_check_in' 		=>	Yii::t("main", "Check In"),
			'rating_review' 		=>	Yii::t("main", "Value"),
		);	
	}

	protected function afterSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$good = $this->object;
				$attributes = array(
					'rating_accuracy',
					'rating_communication',
					'rating_cleanliness',
					'rating_location',
					'rating_check_in',	
					'rating_review'
				);
				$rating_average = 0;
				foreach ($attributes as $attribute) {
					// $good->$attribute = round($good->$attribute + ($this->$attribute * 100) / $good->marksCount) ;
					$good->$attribute = round($good->$attribute + ($this->$attribute * 100 - $good->$attribute) / ($good->marksCount));
					$rating_average += $good->$attribute;
				}
				$good->rating_average = round($rating_average / 6);
				// $good->rating_average = round($good->rating_average + ($rating_average - $good->rating_average) / ($good->marksCount));
				$good->save();
			}
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('object_id',$this->object_id);
		$criteria->compare('text',$this->text);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('rating_accuracy',$this->rating_accuracy);
		$criteria->compare('rating_communication',$this->rating_communication);
		$criteria->compare('rating_cleanliness',$this->rating_cleanliness);
		$criteria->compare('rating_location',$this->rating_location);
		$criteria->compare('rating_check_in',$this->rating_check_in);
		$criteria->compare('rating_review',$this->rating_review);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}